package app.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Scroller;
import android.widget.TextView;
import app.log.APPLog;

import com.longevityassistant.activity.R;

/**
 * [上拉刷新ScrollView]<BR>
 * [支持上拉刷新、完美兼容ListView]
 * @author EX-LILI003
 * @version [Android PABank C01, 2011-12-21] 
 */
public class PullScrollView extends FrameLayout {
	/**
	 * Log日志Tag
	 */
	private static final String TAG = PullScrollView.class.getSimpleName();
	
    /**
	 * 刷新状态，正常
	 */
    public static final int STATE_REFRESH_IDEL = 1;
    
    /**
     * 刷新状态，下拉中
     */
    public static final int STATE_PULL_TO_REFRESH = 2;
    
    /**
     * 刷新状态，松开刷新
     */
    public static final int STATE_RELEASE_TO_REFRESH = 3;
    
    /**
     * 刷新状态，正在刷新
     */
    public static final int STATE_REFRESHING = 4;
    
    /**
     * 刷新状态
     */
    private int mRefreshState;
    
	/**
	 * 内部Scroller
	 */
	private Scroller mScroller;
	
	/**
	 * ScrollView中的内容View
	 */
	private ViewGroup mContentView;
	
	/**
	 * 滑动数据跟踪
	 */
	private VelocityTracker mVelocityTracker;
	
	/**
	 * 最小惯性
	 */
	private int mMinimumVelocity;
	
	/**
	 * 最大惯性
	 */
	private int mMaximumVelocity;
	
	/**
	 * Loading刷新的View
	 */
	private View mLoadView;
	
	/**
	 * 正常Content离顶部高度的距离
	 */
	private int mScrollTop;
	
	/**
	 * 刷新状态箭头动画
	 */
	private Animation mFlipAnimation;
	
	
	/**
	 * 刷新状态箭头反向动画
	 */
	private Animation mReverseFlipAnimation;
	
	/**
	 * 刷新View中，提示TextView
	 */
	private TextView mRefreshViewText;
	
    /**
     * 刷新View中，刷新箭头图片
     */
    private ImageView mRefreshViewImage;
    
    /**
     * 刷新View中，进度条
     */
    private ProgressBar mRefreshViewProgress;
    
    /**
     * 刷新监听器
     */
    private OnRefreshListener mOnRefreshListener;
    
    /**
     * 默认箭头resouce ID
     */
    private int mDefaulArrowResID = R.drawable.app_widget_arrow_loading_down;
    
    /**
     * 是否显示头部View
     */
    private boolean mIsShowRefresh;
    
    /**
     * 是否infalte结束
     */
    private boolean mIsFinishInFlateLayout;
    
    /**
     * 是否支持头部View下拉
     */
    private boolean mIsRefreshPull = true;
    
    /**
     * 头部内容提示信息
     */
    private String mShowTip;
    
    private boolean mShowTopMode;
    
    private int mTopMargin;
    
	/**
     * Touch事件，上一次的TouchY位置
     */
	private float mLastMotionY;
	
	private boolean mIsBeingDragged = false;
	
	private int mTouchSlop;
    
	private float mDensity;
    
	/**
	 * 提示小标签显示提示信息的情况下，任然支持下拉刷新
	 */
	private boolean mIsSingleSupportRefresh;
	
	private boolean mIsSupportUserHead;
	
	/**
	 * [PullScrollView构造]
	 * @param context context
	 */
	public PullScrollView(Context context) {
		super(context, null);
		init(context, null);
	}
	
	/**
	 * [PullScrollView构造]
	 * @param context context
	 * @param attrs attrs
	 */
	public PullScrollView(Context context, AttributeSet attrs) {
		super(context, attrs, 0);
		init(context, attrs);
    }
	
	/**
	 * [PullScrollView构造]
	 * @param context context
	 * @param attrs attrs
	 * @param defStyle defStyle
	 */
	public PullScrollView(Context context, AttributeSet attrs, int defStyle) {
	    super(context, attrs, defStyle);
	    init(context, attrs);
	}

	/**
	 * [初始化]<BR>
	 * [ScrollView初始化]
	 * @param attrs attrs 
	 */
	private void init(Context context, AttributeSet attrs) {
		
		if (attrs != null) {
			TypedArray a = this.getContext().obtainStyledAttributes(attrs,
					R.styleable.PullScrollView);
			mIsShowRefresh = a.getBoolean(R.styleable.PullScrollView_show_refresh, true);
			mIsRefreshPull = a.getBoolean(R.styleable.PullScrollView_refresh_pull, true);
			mShowTip = a.getString(R.styleable.PullScrollView_head_tip);
		}
		
		setFocusable(true);
        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
        setWillNotDraw(false);
        
		RotateAnimation flipAnimation = new RotateAnimation(0, -180,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    	flipAnimation.setInterpolator(new LinearInterpolator());
    	flipAnimation.setDuration(200);
    	flipAnimation.setFillAfter(true);
        mFlipAnimation = flipAnimation;
        
        RotateAnimation reverseFlipAnimation = new RotateAnimation(-180, 0,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        reverseFlipAnimation.setInterpolator(new LinearInterpolator());
        reverseFlipAnimation.setDuration(200);
        reverseFlipAnimation.setFillAfter(true);
        mReverseFlipAnimation = reverseFlipAnimation;
        
        final ViewConfiguration configuration = ViewConfiguration.get(this.getContext());
        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
        mTouchSlop = configuration.getScaledTouchSlop();
        
//		mScroller = new Scroller(this.getContext(), new OvershootInterpolator());
		mScroller = new Scroller(this.getContext(), new DecelerateInterpolator(2.0f));
		
		WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
	    DisplayMetrics displayMetrics = new DisplayMetrics();
		mWindowManager.getDefaultDisplay().getMetrics(displayMetrics);
		mDensity = displayMetrics.density;
//		this.setShowRefresh(mIsShowRefresh);
		
		mTopMargin = getDefaultMarginTop();
	}
	
	@Override 
	protected void onFinishInflate() {
		
		APPLog.v(TAG, "onFinishInflate");
		if (getChildCount() > 0) {
			
			mContentView = (ViewGroup)this.getChildAt(0);

			int paddingBottom = mContentView.getPaddingBottom();
        	if (paddingBottom == 0) {
        		paddingBottom = 500;
        	}
        	int paddingTop = mContentView.getPaddingTop();
        	if (paddingTop == 0) {
        		paddingTop = 500;
        	}
        	
        	mContentView.setPadding(mContentView.getLeft(), 
        			paddingTop, 
        			mContentView.getRight(), 
        			paddingBottom);
        	
			if (mIsShowRefresh) {
				//配置读取后添加Loading View到ScrollView,并做其相应初始
				initLoadingView();
			} else {
				APPLog.i(TAG, "(onFinishInflate)postScrollTop");
				mScrollTop = paddingTop;
				scrollTop();
			}
    		
    		this.setVerticalScrollBarEnabled(false);
        }
		
//		resetScrollTop();
		APPLog.i(TAG, "mScrollTop:" + mScrollTop);
		
		mIsFinishInFlateLayout = true;
	}
	
	/**
	 * [初始化头部LoadView]<BR>
	 */
	private void initLoadingView() {
		APPLog.e(TAG, "initLoadingView");
		//配置读取后添加Loading View到ScrollView,并做其相应初始
    	LayoutInflater inflater = (LayoutInflater)this.getContext()
    				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    	mLoadView = inflater.inflate(R.layout.app_widget_pull_to_refresh_view, null);
    	if(mIsShowRefresh) {
	    	mContentView.addView(mLoadView, 0);
	    	
	    	mLoadView.measure(0, 0);
	    	
	    	mRefreshViewText = (TextView) mLoadView.findViewById(R.id.pull_refresh_text);
	        mRefreshViewImage = (ImageView) mLoadView.findViewById(R.id.pull_refresh_image);
	        mRefreshViewProgress = (ProgressBar) mLoadView.findViewById(R.id.pull_refresh_progress);
	        
	        if (mRefreshViewImage.getDrawable() == null) {
	        	mRefreshViewImage.setImageResource(mDefaulArrowResID);
	        }
	        
	        setRefreshPull(mIsRefreshPull);
	      
	        if(mShowTip!=null){
	        	setHeadTip(mShowTip);
	        }
	        
	        APPLog.i(TAG, "(initLoadingView)resetScrollTop");
	        resetScrollTop(false);
    	} else {
    		mLoadView = null;
    	}
	}
	
	/**
	 * [滑动到头部]<BR>
	 */
	public void postScrollTop() {
		this.post(new Runnable()
		{
			public void run()
			{
				APPLog.i(TAG, "postScrollTop:" + mScrollTop);
				PullScrollView.this.scrollTo(0, mScrollTop);
			}
		});
	}

	public void scrollTop() {
		APPLog.i(TAG, "scrollTop:" + mScrollTop);
		PullScrollView.this.scrollTo(0, mScrollTop);
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {

		final int action = ev.getAction();
        if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
            return true;
        }
        
        final float y = ev.getY();
        
        switch (action) {
            case MotionEvent.ACTION_MOVE:
        		
                final int yDiff = (int) Math.abs(y - mLastMotionY);
                if (yDiff > mTouchSlop) {
                    mIsBeingDragged = true;
                    mLastMotionY = y;
                }
                break;
            case MotionEvent.ACTION_DOWN:
            	mLastMotionY = y;

            	mIsBeingDragged = false;
            	
            	if (!mScroller.isFinished()) {
	                 mScroller.abortAnimation();
	             }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                mIsBeingDragged = false;
                break;
        }
       
        return mIsBeingDragged;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
				
		APPLog.v(TAG, "mScrollTop:" + mScrollTop);
		if (mVelocityTracker == null) {
	       mVelocityTracker = VelocityTracker.obtain();
	    }
	    mVelocityTracker.addMovement(event);
	        
	    int filingBottom = getFilingBottom();
	    
	    int currScrollY = this.getScrollY();
	    int contentTop = mContentView.getPaddingTop() + mTopMargin;
	    int touchY = (int)event.getY();
		
		switch (event.getAction()) {
			case MotionEvent.ACTION_UP:
				APPLog.i(TAG, "mTopMargin" + mTopMargin);
				boolean isAnimation = false;
				
				if (mIsRefreshPull && mIsShowRefresh && currScrollY < contentTop) {
					//滑动到Loading处,开始Loading数据,并且回弹到顶部位置
					APPLog.i(TAG, "滑动到Loading处,开始Loading数据,并且回弹到顶部位置 -- " +
							"filingBottom:" + filingBottom + "|currScrollY:" + currScrollY
							+ "|mScrollTop:" + mScrollTop + "|scrollTo:" + (contentTop - currScrollY));
					if (mIsShowRefresh && mIsRefreshPull && mRefreshState != STATE_REFRESHING) {
						mRefreshState = STATE_RELEASE_TO_REFRESH;
					}
					isAnimation = true;
					mShowTopMode = true;
					mScroller.startScroll(0, currScrollY, 0, contentTop - currScrollY, 500);
					this.invalidate();
				} else if (mRefreshState != STATE_REFRESHING && currScrollY <= mScrollTop && ! mIsSingleSupportRefresh) {
					APPLog.i(TAG, "滑动到了顶部,并且不刷新数据,向下回弹 -- " +
							"filingBottom:" + filingBottom + "|currScrollY:" + currScrollY
							+ "|mScrollTop:" + mScrollTop + "|mIsSingleSupportRefresh:" + mIsSingleSupportRefresh);

					//滑动到了顶部,并且不刷新数据,向下回弹
					isAnimation = true;
					mShowTopMode = false;
					mRefreshState = STATE_REFRESH_IDEL;
					mScroller.startScroll(0, currScrollY, 0, mScrollTop - currScrollY, 500);
					this.invalidate();
				} else {
					
					APPLog.i(TAG, "滑动到底部,向上回弹 -- " +
							"filingBottom:" + filingBottom + "|currScrollY:" + currScrollY
							+ "|mScrollTop:" + mScrollTop);
					mShowTopMode = false;
					//滑动到底部,向上回弹
					if (currScrollY > filingBottom) {
						
						int scrollTo = -Math.abs(currScrollY - filingBottom);
						
						APPLog.i(TAG, "ACTION_UP scrollTo:" + scrollTo);
						
						isAnimation = true;
						if (mRefreshState != STATE_REFRESHING)
							mRefreshState = STATE_REFRESH_IDEL;
						mScroller.startScroll(0, currScrollY, 0, scrollTo, 500);
						this.invalidate();
					}
					
					if (mIsSingleSupportRefresh && mShowTip != null) {
						mRefreshViewImage.setVisibility(View.GONE);
				        mRefreshViewProgress.setVisibility(View.GONE);
				        mRefreshViewImage.setImageDrawable(null);
				        mRefreshViewText.setText(mShowTip);
					}
				}
				
				//加载数据
				if (mRefreshState == STATE_RELEASE_TO_REFRESH) {
					prepareForRefresh();
					if (mOnRefreshListener != null) {
						APPLog.e(TAG, "onRefresh");
				         mOnRefreshListener.onRefresh();
				    }
				}
				
				if (!isAnimation) {
					//fling动作
					final VelocityTracker velocityTracker = mVelocityTracker;
		            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
		            int initialVelocity = (int) velocityTracker.getYVelocity();
		            if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
		                fling(-initialVelocity);
		            }
				}
				
	            if (mVelocityTracker != null) {
	                mVelocityTracker.recycle();
	                mVelocityTracker = null;
	            }
				return true;
			case MotionEvent.ACTION_MOVE:
				if (mIsShowRefresh && mIsRefreshPull && mRefreshState != STATE_REFRESHING) {
					if (currScrollY < contentTop && mRefreshState != STATE_RELEASE_TO_REFRESH) {
						
						//改变状态,松开刷新
						mRefreshViewProgress.setVisibility(View.GONE);
						mRefreshViewImage.setImageResource(mDefaulArrowResID);
						mRefreshViewImage.setVisibility(View.VISIBLE);
						mRefreshViewText.setVisibility(View.VISIBLE);
						mRefreshViewText.setText(R.string.release_more);
			         	mRefreshViewImage.clearAnimation();
			         	mRefreshViewImage.startAnimation(mFlipAnimation);
			         	mRefreshState = STATE_RELEASE_TO_REFRESH;
			         } else if (currScrollY > contentTop && currScrollY < mScrollTop 
			        		 && mRefreshState != STATE_PULL_TO_REFRESH) {
			        	 
			        	//改变状态,下拉刷新
			        	mRefreshViewProgress.setVisibility(View.GONE);
			        	mRefreshViewImage.setVisibility(View.VISIBLE);
			        	mRefreshViewText.setVisibility(View.VISIBLE);
			        	mRefreshViewImage.setImageResource(mDefaulArrowResID);
			         	mRefreshViewText.setText(R.string.pull_more);
		            	mRefreshViewImage.clearAnimation();
		            	mRefreshViewImage.startAnimation(mReverseFlipAnimation);
			            mRefreshState = STATE_PULL_TO_REFRESH;
			            
			        }
				}
				
				//滑动超过顶部后,减慢拖拽速度
				if (currScrollY < mScrollTop || currScrollY > filingBottom) {
					int deltaY = (int)((mLastMotionY - touchY) / 2.2f);
					this.scrollBy(0, deltaY);
		        } else {
		        	int deltaY = (int)(mLastMotionY - touchY);
		        	this.scrollBy(0, deltaY);
		        }
				break;
			case MotionEvent.ACTION_DOWN:
				 if (!mScroller.isFinished()) {
	                 mScroller.abortAnimation();
	             }
				break;
			default:
				break;
		}
		mLastMotionY = touchY;
		return true;
	}

	/**
	 * [刷新数据准备]<BR>
	 * 
	 */
	public void prepareForRefresh() {
	    APPLog.i(TAG, "prepareForRefresh");
        mRefreshViewImage.setVisibility(View.GONE);
        mRefreshViewImage.setImageDrawable(null);
        mRefreshViewProgress.setVisibility(View.VISIBLE);
        mRefreshViewText.setText(R.string.pull_refreshing);

        mRefreshState = STATE_REFRESHING;
	}
	
	/**
	 * [刷新数据]<BR>
	 * [数据加载成功后由外部调用刷新视图]
	 */
	public void refresh() {        
		 
		APPLog.i(TAG, "refresh");
		reset();
		
		int currScrollY = this.getScrollY();
		if (currScrollY < mScrollTop) {
			startAnimTop();
		}
    }
	
	/**
	 * [设置头部View提示加载结束]<BR>
	 */
	public void showLoadComplete() {
		if (mRefreshViewText != null) {
			mRefreshViewText.setText(R.string.pull_load_complete);
		}
	}
	
	public void startAnimTop(){
		APPLog.i(TAG, "startAnimTop");
		int currScrollY = this.getScrollY();
		mScroller.startScroll(0, currScrollY, 0, mScrollTop - currScrollY, 500);
		this.invalidate();
	}
	 
	/**
	 * [重置Loading]<BR>
	 * [刷新数据后Loading会被重置]
	 */
	public void reset() {

		mShowTopMode = false;
		mShowTip = null;
		mRefreshState = STATE_REFRESH_IDEL;
        mRefreshViewImage.setImageResource(mDefaulArrowResID);
        mRefreshViewImage.clearAnimation();
        mRefreshViewImage.setVisibility(View.GONE);
        mRefreshViewText.setText(R.string.pull_more);
        mRefreshViewProgress.setVisibility(View.GONE);
	}
	
	/**
	 * [ScrollView惯性滑动]<BR>
	 * [当使用滑动的时候，根据滑动力度进行滑动]
	 * @param velocityY velocityY
	 */
	public void fling(int velocityY) {
		 APPLog.i(TAG, "fling");
        int limitHeight = 0;
        if (mRefreshState == STATE_REFRESHING) {
        	limitHeight = mContentView.getPaddingTop() + mTopMargin;
        } else {
        	limitHeight = mScrollTop;
        }
        
        int maxY = mContentView.getHeight() - 
        	mContentView.getPaddingBottom() - this.getHeight();
        mScroller.fling(0, this.getScrollY(), 0, velocityY, 0, 0, 
        		limitHeight, maxY);
        this.invalidate();
	}
	 
	
	/**
	 * [ScollView滑动区域的底部位置]<BR>
	 * [根据ScrollView内容的大小，得到ScollView滑动区域的底部位置]
	 * @return int
	 */
	private int getFilingBottom() {
		 
		int contentHeight =  mContentView.getHeight();
		int paddingBottom = mContentView.getPaddingBottom();
		int paddintTop = mContentView.getPaddingTop();
		int height = this.getHeight();
		int trueContentHeight = contentHeight - paddingBottom - paddintTop;
		int curScrollY = this.getScrollY();
		
		if (mIsShowRefresh && mLoadView != null) {
			if (mShowTip != null || mRefreshState == STATE_REFRESHING || mShowTopMode) {
			} else{
				 trueContentHeight -= mLoadView.getHeight();
			}
		}
		APPLog.i(TAG, "trueContentHeight:" + trueContentHeight 
				+ "|contentHeight:" + contentHeight
				+ "|curScrollY:" + curScrollY
				+ "|height:" + this.getHeight());

		if (trueContentHeight <= height) {
			//总高度未填充屏幕
			if (mIsShowRefresh && mRefreshState == STATE_REFRESHING) {
				APPLog.i(TAG, "高度1");
				return paddintTop + mTopMargin;
			} else {
				if (mIsShowRefresh && mShowTip != null || mShowTopMode) {
					APPLog.i(TAG, "高度2");
					return paddintTop + mTopMargin;
				} else {
					if (mLoadView != null) {
						if (mShowTip != null) {
							APPLog.i(TAG, "高度3");
							return paddintTop + mTopMargin;
						} else {
							APPLog.i(TAG, "高度4");
							return paddintTop + mLoadView.getHeight();
						}
					} else if(mIsSupportUserHead) {
						APPLog.i(TAG, "高度5");
						return paddintTop + mTopMargin;
					}else {
						APPLog.i(TAG, "高度5");
						return paddintTop;
					}
				}
			}
		} else {
			return contentHeight - paddingBottom - height;
		}
	}
	
	public void setSupportUserHead(boolean b) {
		mIsSupportUserHead = b;
	}
	
	/**
	 * [设置刷新监听]<BR>
	 * @param onRefreshListener 监听器
	 */
	public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
		mOnRefreshListener = onRefreshListener;
	}
	 
	@Override
    protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
		
        ViewGroup.LayoutParams lp = child.getLayoutParams();

        int childWidthMeasureSpec;
        int childHeightMeasureSpec;

        childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, this.getPaddingLeft()
                + this.getPaddingRight(), lp.width);

        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

    @Override
    protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
            int parentHeightMeasureSpec, int heightUsed) {
    	
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                this.getPaddingLeft() + this.getPaddingRight() + lp.leftMargin + lp.rightMargin
                        + widthUsed, lp.width);
        final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }
    
    /**
     * [设置是否显示头部Refresh控件]<BR>
     * @param b 是否支持
     */
    public void setShowRefresh(boolean b) {
    	
		mIsShowRefresh = b; 
		
		if (mIsFinishInFlateLayout) {
			if (mIsShowRefresh) {
				if (mLoadView == null) { 
					initLoadingView();
				}
			} else {
				if (mLoadView != null) {
					mContentView.removeView(mLoadView);
					mLoadView = null;
					mScrollTop = mContentView.getPaddingTop();
					setTopMargin(0);
				}
			}
		}
	}
    
    public int getDefaultMarginTop() {
    	return (int)(20 * mDensity);
    }
    
    /**
     * [设置顶部View]<BR>
     * @param view
     */
    public void setLoadView(View view){
    	mLoadView = view;
    }
	
	/**
	 * [是否显示了头部Refresh控件]<BR>
	 * @return boolean
	 */
	public boolean isShowRefresh() {
		return mIsShowRefresh;
	}
	
	/**
	 * [是否完成infalteLayout操作]<BR>
	 * @return boolean
	 */
	public boolean isFinishInFlateLayout() {
		return mIsFinishInFlateLayout;
	}
	
	/**
	 * [头部Refresh控件是否需要被拖拽]<BR>
	 * [只有设置了显示头部的情况下才能有作用]
	 * @param isPull 是否支持Pull
	 */
		
	public void setRefreshPull(boolean isPull) {
		mIsRefreshPull = isPull;
		
		if (isPull) {
			if (mLoadView != null) {
				mScrollTop = mLoadView.getMeasuredHeight() + mContentView.getPaddingTop();
			}
		} else {
			APPLog.i(TAG, "setRefreshPullmContentView.getPaddingTop():" + mContentView.getPaddingTop());
			mScrollTop = mContentView.getPaddingTop() ;
		}
	}
	
	/**
	 * [是否支持头部Pull]<BR>
	 * @return boolean
	 */
	public boolean isRefreshPull() {
		return mIsRefreshPull;
	}
	
	/**
	 * [设置Head控件的提示信息]<BR>
	 * [只有设置了显示头部的情况下才能有作用]
	 * @param tip 提示信息
	 */
	public void setHeadTip(String tip) {
		setHeadTip(tip, getDefaultMarginTop());
	}
	
	public void setHeadTip(String tip, int topMargin) {
		
		mShowTip = tip;
		
		if (tip != null) {
			mRefreshViewImage.setVisibility(View.GONE);
			mRefreshViewImage.setImageDrawable(null);
	        mRefreshViewProgress.setVisibility(View.GONE);
	        mRefreshViewText.setText(tip);
	        
	        mRefreshState = STATE_REFRESH_IDEL;
	        
	        setTopMargin(topMargin);
		} else {
			
			setTopMargin(0);
		}
	}
	
	public void setTopMargin(int topMargin) {
		mTopMargin = topMargin;
	}
	
	public void setTipAndRefresh(String tip){
		setHeadTip(tip);
		setSingleSupportRefresh(true);
	}
    
	/**
	 * [重置ScrollView的顶部位置]<BR>
	 * @param isPostRefresh
	 */
	public void resetScrollTop(boolean isPostRefresh) {
		if (! mIsSupportUserHead) {
			if (mIsShowRefresh) {
				if (!mIsRefreshPull && mShowTip != null ) {
					//位置包括顶部小标签，并且显示一半
					mScrollTop = mContentView.getPaddingTop() + mTopMargin;
					APPLog.i(TAG, "resetScrollTop 1 - " + mScrollTop);
				} else if (mIsShowRefresh && mIsRefreshPull && mLoadView != null && mIsSingleSupportRefresh) {
					mScrollTop = mContentView.getPaddingTop() + mTopMargin;
				} else if (mIsRefreshPull && mLoadView != null) {
					//不包括顶部小标签
					mScrollTop = mContentView.getPaddingTop() + mLoadView.getMeasuredHeight();
					APPLog.i(TAG, "resetScrollTop 2 - " + mScrollTop);
				}else {
					mScrollTop = mContentView.getPaddingTop();
					APPLog.i(TAG, "resetScrollTop 3 - " + mScrollTop);
				}
				
			} else {
				mScrollTop = mContentView.getPaddingTop();
				APPLog.i(TAG, "resetScrollTop 4 - " + mScrollTop);
			}
		} else {
			mScrollTop = mContentView.getPaddingTop() + mTopMargin;
		}

		if (isPostRefresh) {
			APPLog.i(TAG, "(resetScrollTop)postScrollTop");
			postScrollTop();
		} else {
			//罗春专属
			//is luo chun
			scrollTop();
		}
	}
	
	public void resetScrollTop() {
		resetScrollTop(true);
	}
	
	/**
	 * [设置提示小标签显示提示信息的情况下，任然支持下拉刷新模式]<BR>
	 * @param b
	 */
	public void setSingleSupportRefresh(boolean b) {
		mIsSingleSupportRefresh = b;
	}
	
	 @Override
	public void computeScroll() {
		 if (mScroller.computeScrollOffset()) {
			 PullScrollView.this.scrollTo(0, mScroller.getCurrY());
		 }
	}
	 
	public void removeAllChild() {

		if (mLoadView != null) {
			int childCount = mContentView.getChildCount();
			for (int i = 0;i < childCount; i ++) {
				mContentView.removeViewAt(0);
			}
			initLoadingView();
		} else {
			super.removeAllViews();
		}
	}
	
	public void addContentChild(View view) {
		if (mContentView != null) {
			mContentView.addView(view);
		}
	}
	
	public int getRefreshState() {
		return mRefreshState;
	}
	
	public interface OnRefreshListener {
		/**
		 * [刷新数据事件]<BR>
		 * [当松开刷新的时候触发该事件]
		 */
		public void onRefresh();
	}
}
